<template>
  <el-container class="mainContent">
    <el-row>
      <el-col class="title">签约居民列表</el-col>
    </el-row>
    <el-row class="search-layer">
      <el-col>
        <el-form :inline="true" :model="formSearch" class="demo-form-inline">
          <el-form-item label="家庭医生姓名:">
            <el-input v-model="formSearch.doctorName" placeholder="请输入医生姓名"></el-input>
          </el-form-item>
          <el-form-item label="签约状态:">
            <el-select v-model="formSearch.status" placeholder="请选择签约状态">
              <el-option label="签约" value="1"></el-option>
              <el-option label="解约" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="searchGroup">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-circle-plus-outline" @click="add">添加</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <el-table :data="groupData" stripe style="width: 100%">
        <el-table-column prop="c_name" label="居民姓名" width="150"></el-table-column>
        <el-table-column prop="c_sex" label="性别" width="100"></el-table-column>
        <el-table-column prop="c_sid" label="身份证号码" width="200"></el-table-column>
        <el-table-column prop="c_tel" label="电话" width="150"></el-table-column>
        <el-table-column prop="c_sid" label="出生日期" width="150">
          <template slot-scope="scope">
            <span>{{ getDate(scope.row.c_sid)}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="c_type" label="重点人群类型" width="200">
          <template slot-scope="scope">
            <span v-for="(item,index) in scope.row.c_type" :key="index">{{item}}&nbsp;</span>
          </template>
        </el-table-column>
        <el-table-column prop="c_teamManager" label="责任团队" width="200"></el-table-column>
        <el-table-column prop="c_doctorManager" label="签约医生" width="200"></el-table-column>
        <el-table-column prop="c_contractState" label="签约状态" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.c_contractState == '1'">签约</span>
            <span v-else style="color: red">解约</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
        class="pagination"
      ></el-pagination>
    </el-row>
  </el-container>
</template>
<script>
export default {
  name: "ContractManagement",
  data() {
    return {
      formSearch: {
        doctorName: "",
        status: "",
      },
      groupData: [],
      currentPage: 1,
      count: 0,
      num: 10,
    };
  },
  methods: {
    getDate(date) {
      // console.log(date)
      let year = date.substring(6, 10);
      let mon = date.substring(10, 12);
      let day = date.substring(13, 15);
      return `${year}-${mon}-${day}`;
    },
    getData() {
      this.axios({
        url: "/getContractManager",
        methods: "get",
        params: {
          page: this.currentPage,
          num: this.num,
          doctorName: this.formSearch.doctorName,
          status: this.formSearch.status,
        },
      })
        .then((res) => {
          console.log(res);
          if (res.data.code === 200) {
            this.groupData = res.data.data;
            this.count = res.data.count;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    searchGroup() {
      this.getData();
    },
    add() {},
    handleEdit(index, row) {
      console.log(index, row);
      sessionStorage.setItem("c_id",row.c_id)
      this.$router.push("/home/contract/editContract")
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("是否删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios({
            url: "/delContractManager",
            method: "post",
            data: { c_id: row.c_id },
          }).then((res) => {
            console.log(res);
            if (res.data.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getData()
            }else{
                 this.$message.error('删除失败');
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.num = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
      //   console.log(`当前页: ${val}`);
    },
  },
  mounted() {
    this.getData();
  },
};
</script>
<style lang="less" scoped>
.text-red {
  color: red;
}
.form-inline {
  width: 100%;
  height: 300px;
  text-align: left;
}
.mainContent {
  width: 100%;
  flex-direction: column;
  text-align: left;
}
.search-layer {
  width: 100%;
}
.pagination {
  margin-top: 50px;
  text-align: center;
}
.title {
  margin-bottom: 30px;
  font-size: 20px;
  font-weight: 600;
}
</style>